package com.celesea.workflow;

import com.celesea.framework.authority.User;
import com.celesea.framework.util.EnvUtil;
import com.celesea.workflow.core.entity.WorkflowInstance;
import com.celesea.workflow.core.entity.WorkflowNotice;
import com.celesea.workflow.core.entity.WorkflowStatus;

import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JackYuan
 * @create 2017-11-21 18:45
 */
public class WorkflowNoticeFactory {

    private static WorkflowNotice buildWorkflowNotice(WorkflowInstance entity) {
        WorkflowNotice workflowNotice = new WorkflowNotice();
        workflowNotice.setInstanceId(entity.getId());
        workflowNotice.setWorkflowType(entity.getWfType());
        workflowNotice.setSummary(entity.getSummary());
        User user = EnvUtil.getLoginUser();
        workflowNotice.setOwnerId(user.getId());
        workflowNotice.setOwnerName(user.getName());
        workflowNotice.setNoticeTime(Calendar.getInstance().getTime());
        return workflowNotice;
    }

    public static WorkflowNotice draftNotice(WorkflowInstance entity) {
        WorkflowNotice workflowNotice = buildWorkflowNotice(entity);
        workflowNotice.setWorkflowStatus(WorkflowStatus.DRAFT.getCode());
        return workflowNotice;
    }

    public static List<WorkflowNotice> processNotice(
            WorkflowInstance entity, List<User> users) {
        return users.stream().map(user -> {
            WorkflowNotice workflowNotice = new WorkflowNotice();
            workflowNotice.setInstanceId(entity.getId());
            workflowNotice.setWorkflowStatus(entity.getWorkflowStatus());
            workflowNotice.setWorkflowType(entity.getWfType());
            workflowNotice.setSummary(entity.getSummary());
            workflowNotice.setOwnerId(user.getId());
            workflowNotice.setOwnerName(user.getName());
            workflowNotice.setNoticeTime(Calendar.getInstance().getTime());
            return workflowNotice;
        }).collect(Collectors.toList());

    }

    public static WorkflowNotice completeNotice(WorkflowInstance entity) {
        WorkflowNotice workflowNotice = buildWorkflowNotice(entity);
        workflowNotice.setWorkflowStatus(WorkflowStatus.COMPLETE.getCode());
        return workflowNotice;
    }
}
